package cn.zjj.user.controller;

import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.util.JsonResult;
import cn.zjj.basic.util.PageList;
import cn.zjj.user.domain.User;
import cn.zjj.user.dto.UserPhoneRegisterDto;
import cn.zjj.user.query.UserQuery;
import cn.zjj.user.service.IUserService;
import cn.zjj.user.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "普通用户的API",description="普通用户相关的CRUD功能")
public class UserController {

    @Autowired
    private IUserService userService;
    //用户注册
    @PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody UserPhoneRegisterDto userPhoneRegisterDto){
        try {
            userService.phoneRegister(userPhoneRegisterDto);
            return JsonResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return new JsonResult().runtimeException(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"系统繁忙,稍后重试");
        }
    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    public List<User> selectAll(){

        return userService.selectAll();
    }

    //查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据" )
    public User selectById(@PathVariable("id") Long id){
        return userService.selectById(id);
    }

    //删除单个
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个数据" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return JsonResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return new JsonResult().runtimeException(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //添加或修改
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
    public JsonResult updateOrAdd(@RequestBody User user){
        try {
            if(user.getId()==null){
                userService.insert(user);
            }else {
                userService.update(user);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询或高级查询" )
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.selectQuery(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult delete(@RequestBody List<Long> ids){
        try {
            userService.patchDel(ids);
            return JsonResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return new JsonResult().runtimeException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }
}
